package com.snopy.concurrency.atomic;

import com.fasterxml.jackson.databind.ser.std.StdJdkSerializers;
import lombok.extern.slf4j.Slf4j;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Demonstration
 * @description
 * @email 77912204@qq.com
 * @date 2022/06/28 15:48
 */
@Slf4j
public class AtomicMarkedDemo {

    public static void main(String[] args) throws InterruptedException {
        Node C = new Node(3,null);
        Node B = new Node(2,C);
        Node A = new Node(1,B);

        Thread t1 = new Thread(()->{
            //先将B点标记为逻辑删除
            B.next.set(B.next.getReference(), true);
            Node prev = A.next.getReference();//2
            Node next = B.next.getReference();//3

            try {
                Thread.sleep(3000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }

            log.debug("delete 2:{}",A.next.compareAndSet(prev,next,false,false));
        });
        t1.start();
        Thread.sleep(2000);
        Thread t2 = new Thread(()->{
            C.next.set(C.next.getReference(), true);
            Node prev = B.next.getReference();//3
            boolean bf = B.next.isMarked();
            Node next = C.next.getReference();//null
            log.debug("b.flag:{},delete 3:{}",bf,B.next.compareAndSet(prev,next,false,false));
            //
        });
        t2.start();
        t1.join();
        t2.join();
        Node root = A;
        while (root!=null){
            boolean [] marked = new boolean[1];
            root.next.get(marked);
            log.info("{},{}",root.value,marked[0]);
            root = root.next.getReference();
        }
    }
}
